var express = require('express');
var router = express.Router();
const User = require("../beans/user")
const userController = require("../controllers/user_controller")
const UserInfo = require("../beans/userinfo")
/* GET users listing. */
const mongoUtil = require("../models/mongo_util")
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

// 注册
router.post("/register", (req, res) => {
  console.log("/register", req.body.username);

  // 获取注册的用户名和密码
  const user = new User(
    req.body.username,
    req.body.password
  )

  // 将用户的注册请求交给总部定夺
  userController.doRegister(user, (result) => {
    res.json(result)
  })
})
// 登录
router.post('/login', function (req, res, next) {
  // 获取注册的用户名和密码
  const user = new User(
    req.body.username,
    req.body.password
  )
  // 调用控制层执行登录
  userController.doLogin(user, (result) => {
    // 登录成功时为客户端写入一个cookie: loginok=true
    if (result.code == 200) {
      class Token {
        constructor(Token) {
          this.Token = Token
        }
      }
      class WhereStr {
        constructor(_id) {
          this._id = _id

        }
      }
      const whereStr = new WhereStr(result.data._id)
      const token = new Token(
        result.data.token,
      )
      var updateStr = {
        $set: {
          'userid':result.data._id.toString(),
          token
        }
      };
      console.log(updateStr);
      mongoUtil.getCollection("user").then(collection => {

        collection.updateOne(whereStr, updateStr, function (err, res) {
          console.log("加入成功");
        });
      })

    }
    console.log("nihaohao", result);
    res.json(result)
  })

});

// 查看用户信息
// user/:uid
// const loginChecker = require("../middlewares/login_checker")
// const token = createToken({}, 10)
router.post("/info", (req, res) => {
  // const user = new User(
  //   req.body.userid,
  //   // req.query.total=true
  //   // req.body.token
  // )
  const token = {
    Token: req.body.token
  }

  // const token=req.body.token
  console.log("token", token);
  const user = new UserInfo(
    token,
    // req.body.token
    // userid
  )
  // console.log("usereerer",user);
  userController.doUserInfoQuery(user, (result) => {
    res.json(result)
  })

})
// 后台查询所有用户
router.post("/Alluser", (req, res) => {
  var {page,pagesize}=req.body
 
  userController.getuserAlllist({page,pagesize}, (result) => {
    res.json(result)
  })

})

//修改用户信息
// var userInfo = {
//   "nickName": "15866669999",
//   "loginName": "15866669999",
//   "introduceSign": "哈哈哈哈"
// }
router.put("/info", (req, res) => {
  var {
    userid,
    userInfo
  } = req.body

  userController.doputUserInfo(userid, (result) => {
    if (result.code == 200) {
      result.data['userInfo'] = userInfo
    }
    var whereStr = {
      'userid': userid,
      // 'goodsId':goodsId
    }
    console.log("result.data=====",result.data);
    // res.json(result)
 
    var updateStr = {
      $set: {
        'userInfo':userInfo
      }
    };
    mongoUtil.getCollection("user").then(collection => {

      collection.updateOne(whereStr, updateStr, function (err, res) {
        console.log("修改成功");
      });
    })
    res.json({code:200,data:[],msg:"修改成功"})
  })
})
module.exports = router;